package carassius.DAL;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author siebz0r
 */
public class WaterConditionRow
		extends Row<String>
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private String _name, _description;
	private final String SQL_UPDATE_DESCRIPTION = "UPDATE water_condition SET description = ? where name = ?";
	private final Object DESCRIPTION_LOCK;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public WaterConditionRow(ResultSet resultSet) throws SQLException
	{
		super(resultSet.getString("name"));
		_name = resultSet.getString("name");
		_description = resultSet.getString("description");
		DESCRIPTION_LOCK = new Object();
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Properties">
	public String getName()
	{
		return _name;
	}

	public String getDescription()
	{
		synchronized (DESCRIPTION_LOCK)
		{
			return _description;
		}
	}

	public boolean setDescription(String description)
	{
		boolean result = false;
		synchronized (DESCRIPTION_LOCK)
		{
			if (description == null ? _description != null : !description.equals(_description))
			{
				try
				{
					PreparedStatement st = Database.prepareStatement(SQL_UPDATE_DESCRIPTION);
					if (description != null)
					{
						st.setString(1, description);
					}
					else
					{
						st.setNull(1, java.sql.Types.VARCHAR);
					}
					st.setString(2, _name);

					if (st.executeUpdate() == 1)
					{
						_description = description;
						result = true;
					}
					st.close();
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
		return result;
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	@Override
	public boolean equals(Object obj)
	{
		if (obj == null)
		{
			return false;
		}
		if (getClass() != obj.getClass())
		{
			return false;
		}
		final WaterConditionRow other = (WaterConditionRow) obj;
		if ((this._name == null) ? (other._name != null) : !this._name.equals(other._name))
		{
			return false;
		}
		if ((this._description == null) ? (other._description != null) : !this._description.equals(other._description))
		{
			return false;
		}
		return true;
	}

	@Override
	public int hashCode()
	{
		int hash = 5;
		return hash;
	}

	@Override
	public String toString()
	{
		return _name;
	}
	//</editor-fold>
}
